package geektime.spring.springbuks.config;

import com.alibaba.druid.pool.DruidDataSource;
import geektime.spring.springbuks.model.DruidProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "geektime.spring.springbuks.dao.db2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DB2DataSourceConfig  {

    @Bean("db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.db2") //璇诲彇application.yml涓殑閰嶇疆鍙傛暟鏄犲皠鎴愪负涓�涓璞�
    public DataSource getDataSource(DruidProperties druidProperties){
//        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
//        return druidProperties.dataSource(dataSource);
        return null;
    }

    @Bean("db2SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("db2DataSource") DataSource ds) {
        return new DataSourceTransactionManager(ds);
    }


}